package transactions.dq.mail;

import java.util.Vector;

import dinamica.GenericTransaction;
import dinamica.Recordset;

public class GenericMailPart extends GenericTransaction{

	public int service(Recordset inputParams) throws Throwable 
	{
		int rc = super.service(inputParams);
		
		String mailTo = inputParams.getString("mail_to");
		String mailCC = inputParams.getString("mail_cc");
		String subject = inputParams.getString("subject");
		String body = inputParams.getString("body");
		String[] fileNames = getRequest().getParameterValues("file_name");
		Vector<String> file = new Vector<String>();
		if(fileNames != null && fileNames.length > 0){
			for(int i=0;i<fileNames.length;i++){
				file.add(fileNames[i]);
			}
		}
		
		String host = getMailServerAddress();
		String hostPort = getEmailPort();
		String mailFrom = getEmailFrom();
		String mailFromPass = getEmailFromPass();
		String mailFromDisplayName = getEmailFromDisplayName();
		
		
		//enviar email usando un thread en background
		SimpleMail s = new SimpleMail();
		s.send(host, hostPort, mailFrom, mailFromPass, mailFromDisplayName, mailTo, subject, body, mailCC, file);
		
		return rc;
	}
	
	public String getEmailFromPass() throws Throwable
	{
		String value = getContext().getInitParameter("mail-from-pass");
		if (value==null || value.equals(""))
			throw new Throwable("Par�metro inv�lido en web.xml: mail-from-pass");
		return value;
	}
	public String getEmailPort() throws Throwable
	{
		String value = getContext().getInitParameter("mail-port");
		if (value==null || value.equals(""))
			throw new Throwable("Par�metro inv�lido en web.xml: mail-port");
		return value;
	}
}
